Predicting a songs album


Tempograms


As you can see the cyclic vs non cyclic tempo analysis has a very different estimation of BPM. According to https://getsongbpm.com/song/kim/jq1ooB Kim has a BPM of 142, whcih is more in line with the cyclic estimation. I want to know what exactly casuses the differences between these estimation, so if you do please leave it in the review :).

I chose for Kim, since it’s an old song of eminem and can be seen as an outlier, since the song is critisized as going lyrically to far. Other songs i have tried where also more stable than this one, a possible explanation for this consistency could be the rap genre. Rap is often consistent in BPM, since the rhythm and beat is used to spit raps to, so clear lines where expected.

further more running the grams where intense for my machine by utilizing 100% of my ram for 30 minutes straight as seen in the attachment. I want to do more test runs, but before i do that i need to install more ram :)


Eminem

Key vs chorodgram analysis of Mockingbird vs outlier Tone deaf


Explanation of Mockingbird and Tone deaf.

  • The keygram for mockingbird is a lot clearer than the chordogram, Mockingbird is know for its piano play, which you can clearly follow over time.
  • The grams for tone deaf are totally different and don’t seem correct. However tone deaf is I believe the only number of eminem where he doesn’t change his sound throughout the song. The interesting thing woth tone deaf is that the same chords are repeated the entire length of the song.

timbre and self similarity


week8 text

  • figure 1 shows Self-similarity matrices, segmented by bars, with the axes in seconds of the song mockingbird. The timbre based matrix expresses the blocks that can also be seen in chroma more vividly. around the 90 second mark you see a switch with the timbre, this is also where the refrain of mockingbird starts and more instruments chime in. around the 300 mark the songe ends which can be seen in the chroma matrix, if you look at the crossing yellow lines.
  • In figure 2 I wanted to see if a number by Eminem called Tone deaf would yield interesting or different results, and it sure did. This number is one of the few songs of Eminem where the beat stays consistent and the matrices show this.

Introduction

TODO: Tekst readme plakken Computational musicology Corpus idea:

How did famous rapper Eminem’s style change through te years. From his firsts single "I just dont give a f*ck" in 1998 to one of hist latest songs “The adventures of moon man and slim shady” in 2020.

I chose this corpus, since Eminem is one of my favorite artists of all time. I find myself coming back to his songs on the regular and even went to his last tour in the Netherlands. Eminem has many singles and albums published through the years and all are surounded by some sort of controversion. Eminem even raps about the fact that some of his fans want the old slim shady back, but that he is not that person anymore. I want to check in what degree this statement holds when comparing his music through the years.

Eminem’s total of eleven studio albums make a great music library to compare on various aspects. In these albums Eminem changed up his rap game by collabing with artist and go for a more mainstream pop song. I want to see if this has happened more later on or if he did this before as well. Furthermore eminem is known for his many words per second in songs such as “rap god” or “Godzilla”

A possiblie weakness for the corpus might eb that some songs appear on multiple albums. On the one hand this shows that an artist is proud of a certain song and places it in more places. On the other hand i might have to look at the representations of these songs, to not get an unfair advantage or skew the data.


Main focus points


Eminem

Radarplot


This is a radarchart which represents all the mean values of eminems songs.

  • It is interesting that instrumentalness is almost non-existent.

  • Energy and danceability are high.

  • Speechiness is low, this can be explained by eminemns gimmick. Namely rapping very fast, which is hard to replicate.

  • Most of his albums share the same charcteristics, mainly differing in energy.

Chroma of Mockingbird


Mocking bird

  • Mostly played in C D and E

Results


Notable things

  • Many skits are not popular, still eminem keeps them in his album. This might be since most albums have a story behind them and the skit shows some light on this backstory. An example is the skits with his manager Paul.

  • Most of his big albums have atleast one popular song. indicating that he is still performing well even after all those years.

  • WHat i still want to do is comapare different albums, especially one of his first vs one of his last and see if there are big differences between them. Sugestions on visualisation are welcome :)

---
title: " My portfolio"
author: "Jens Ruhof"
date: "2/20/2021"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(spotifyr)
library(dplyr)
library(fmsb)
library(png)
library(plotly)
library(compmus)
library(gridExtra)
library(cowplot)
library(forcats)
library(knitr)
library(ggpubr)
library(tidymodels)
library(ggdendro)
library(heatmaply)
```

``` {r get_playlist, include=FALSE}
# Eminem overall
 Eminem <- get_playlist_audio_features("", "6E8sLG3bm1meTRj8n5l0ll")
mean_eminem <- Eminem %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```


``` {r get_playlist_lp, include=FALSE}
# Shady lp
shady_lp <- get_playlist_audio_features("", "35sidb3NwfwV5QYnri3yLv")
mean_shadylp <- shady_lp %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```


``` {r get_playlist_xv, include=FALSE}
shadyxv <- get_playlist_audio_features("", "3oJ5M47mmYILrqHyzuXctM")
mean_shadyxv <- shadyxv %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_rev, include=FALSE}
revival <- get_playlist_audio_features("", "7yqbChSYPODqONRuAw4noI")
mean_revival <- revival %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_ref, include=FALSE}
relapse_refill <- get_playlist_audio_features("", "1xyhz9gNz5jJIVTOA9v6Dh")
mean_relapse_refill <- relapse_refill %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_del, include=FALSE}
relapse_deluxe <- get_playlist_audio_features("", "1aQ1zZBcyBFDwsuOdm19cz")
mean_relapse_deluxe<- relapse_deluxe %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_rec, include=FALSE}
recovery <- get_playlist_audio_features("", "2FDhk6jh6iQUDCc7n73diZ")
mean_recovery<- recovery %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_ma, include=FALSE}
murdera <- get_playlist_audio_features("", "69NMqLx5Kszkwl0buUEoBA")
mean_murdera<- murdera %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_mb, include=FALSE}
murderb <- get_playlist_audio_features("", "5ZUIc7iAkEBBeEXzyj7eia")
mean_murderb<- murderb %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_show, include=FALSE}
eminemshow <- get_playlist_audio_features("", "5g2QlPXMIoH4j34xQiHE1J")
mean_eminemshow <- eminemshow %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_matherlp, include=FALSE}
matherlp<- get_playlist_audio_features("", "1ILmjmVb32khVNXG2Zw7kp")
mean_matherlp <- matherlp %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_matherlp2, include=FALSE}
matherlp2<- get_playlist_audio_features("", "2kmroX8obtAPEYaVPAaVHi")
mean_matherlp2 <- matherlp2 %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_kamikaze, include=FALSE}
kamikaze<- get_playlist_audio_features("", "6B5em5RI6brFjyK7eyos1h")
mean_kamikaze <- kamikaze %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r get_playlist_encore, include=FALSE}
encore<- get_playlist_audio_features("", "5VdzQrI0NliDdfwjQ0rPLN")
mean_encore <- encore %>%
  summarise(
    mean_danceability = mean(danceability),
    mean_energy = mean(energy),
    mean_speechiness = mean(speechiness),
    mean_acousticness = mean(acousticness),
    mean_instrumentalness = mean(instrumentalness),
    mean_liveness = mean(liveness),
    mean_valence = mean(valence)
    
  )
```

``` {r}
get_conf_mat <- function(fit) {
  outcome <- .get_tune_outcome_names(fit)
  fit %>% 
    collect_predictions() %>% 
    conf_mat(truth = outcome, estimate = .pred_class)
}

```

### Predicting a songs album
``` {r}
eminem_albums <-
  bind_rows(
    # shady_lp %>% mutate(playlist = "Shady_lp") %>% slice_head(n = 20),
    # shadyxv %>% mutate(playlist = "Shady XV") %>% slice_head(n = 20),
    # revival %>% mutate(playlist = "Revival") %>% slice_head(n = 20),
    # relapse_refill %>% mutate(playlist = "Relapse refill") %>% slice_head(n = 20),
    # relapse_deluxe %>% mutate(playlist = "Relapse Deluxe") %>% slice_head(n = 20),
    recovery %>% mutate(playlist = "Recovery") %>% slice_head(n = 20),
    # murdera %>% mutate(playlist = "Mustic to be murdered by A") %>% slice_head(n = 20),
    # murderb %>% mutate(playlist = "Mustic to be murdered by B") %>% slice_head(n = 20),
    eminemshow %>% mutate(playlist = "The Eminem Show") %>% slice_head(n = 20),
    # matherlp %>% mutate(playlist = "Mather LP") %>% slice_head(n = 20),
    matherlp2 %>% mutate(playlist = "Mather LP2") %>% slice_head(n = 20),
    kamikaze %>% mutate(playlist = "Kamikaze") %>% slice_head(n = 20)
    # encore %>% mutate(playlist = "Encore") %>% slice_head(n = 20)
    
  ) 
```

``` {r classify features}
eminem_features <-
  eminem_albums %>%  # For your portfolio, change this to the name of your corpus.
  add_audio_analysis() %>% 
  mutate(
    playlist = factor(playlist),
    segments = map2(segments, key, compmus_c_transpose),
    pitches =
      map(
        segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"
      ),
    timbre =
      map(
        segments,
        compmus_summarise, timbre,
        method = "mean",
      )
  ) %>%
  mutate(pitches = map(pitches, compmus_normalise, "clr")) %>%
  mutate_at(vars(pitches, timbre), map, bind_rows) %>%
  unnest(cols = c(pitches, timbre))

```


``` {r classify recipe}
eminem_recipe <-
  recipe(
    playlist ~
      danceability +
      energy +
      loudness +
      speechiness +
      acousticness +
      instrumentalness +
      liveness +
      valence +
      tempo +
      duration +
      C + `C#|Db` + D + `D#|Eb` +
      E + `F` + `F#|Gb` + G +
      `G#|Ab` + A + `A#|Bb` + B +
      c01 + c02 + c03 + c04 + c05 + c06 +
      c07 + c08 + c09 + c10 + c11 + c12,
    data = eminem_features,          # Use the same name as the previous block.
  ) %>%
  step_center(all_predictors()) %>%
  step_scale(all_predictors())      # Converts to z-scores.
  # step_range(all_predictors())    # Sets range to [0, 1].
```

``` {r cross}
eminem_cv <- eminem_features %>% vfold_cv(5)
```


``` {r forest}
forest_model <-
  rand_forest() %>%
  set_mode("classification") %>% 
  set_engine("ranger", importance = "impurity")
eminem_forest <- 
  workflow() %>% 
  add_recipe(eminem_recipe) %>% 
  add_model(forest_model) %>% 
  fit_resamples(
    eminem_cv, 
    control = control_resamples(save_pred = TRUE)
  )

```

``` {r forest pred}
# eminem_forest %>% get_pr()

```

``` {r forest mosaic}
eminem_forest %>% get_conf_mat() %>% autoplot(type = "mosaic")
```


``` {r}
workflow() %>% 
  add_recipe(eminem_recipe) %>% 
  add_model(forest_model) %>% 
  fit(eminem_features) %>% 
  pluck("fit", "fit", "fit") %>%
  ranger::importance() %>% 
  enframe() %>% 
  mutate(name = fct_reorder(name, value)) %>% 
  ggplot(aes(name, value)) + 
  geom_col() + 
  coord_flip() +
  theme_minimal() +
  labs(x = NULL, y = "Importance")

```


*** 

- For classification I chose four well known EMinem albums that have mean outliers in comparisson with its other albums. This selection has been made based on the interactive radarplot. 
Recovery has a strong outlier in mean energy. The eminem Show has relatively high mean instrumentalness compared to the other albums. The marshal mathers LP has high mean energy and a high mean liveliness. Kamikaze has a high mean danceability. I expect my random forest to perform well based on these observations. It might struggle with recovery and the marshal amthers lp 2, since both of them score high on mean energy. For recipe I went with everything. 

- The results are in line with my expectations, namely that the eminem show and recovery would classify well. I expected this, since their values stood out. I also expected to see an overlap between the amthers lp2 and recovery, since the energy of the LP is very similar to recovery. However I expected a clearer division, because of the liveliness difference. Interesting enough the points on which i found the classification would go aren't the most important features for our forest.

- I believe this is due the outliers in every album that do not correspond with the mean values that can be seen in teh plot. The Eminem Show with the outlier in instrumentalness performs well and I think this is because of this outlier.



### Tempograms
``` {r load tempos, cache = TRUE}
graveola <- get_tidy_audio_analysis("20oenBXlmwIfK0F3fQIjhM")

cyclic <- graveola %>%
  tempogram(window_size = 8, hop_size = 1, cyclic = TRUE) %>%
  ggplot(aes(x = time, y = bpm, fill = power)) +
  geom_raster() +
  scale_fill_viridis_c(guide = "none") +
  labs(x = "Time (s)", y = "Tempo (BPM)") +
  theme_classic()

non_cyclic <- graveola %>%
  tempogram(window_size = 8, hop_size = 1, cyclic = FALSE) %>%
  ggplot(aes(x = time, y = bpm, fill = power)) +
  geom_raster() +
  scale_fill_viridis_c(guide = "none") +
  labs(x = "Time (s)", y = "Tempo (BPM)") +
  theme_classic()

```

``` {r plotting, cache = TRUE}
grid.arrange (arrangeGrob(cyclic, top=grid::textGrob("cyclic", hjust = 0.15), left="Kim"),arrangeGrob(non_cyclic,top=grid::textGrob("non-cyclic", hjust = 0.21)),ncol=2, nrow=1, widths = c(2.2,2))
```


***

As you can see the cyclic vs non cyclic tempo analysis has a very different estimation of BPM.
According to https://getsongbpm.com/song/kim/jq1ooB Kim has a BPM of 142, whcih is more in line with the
cyclic estimation. I want to know what exactly casuses the differences between these estimation, so if you do please leave it in the review :).

I chose for Kim, since it's an old song of eminem and can be seen as an outlier, since the song is critisized as going lyrically to far. Other songs i have tried where also more stable than this one, a possible explanation for this consistency could be the rap genre. Rap is often consistent in BPM, since the rhythm and beat is used to spit raps to, so clear lines where expected.

further more running the grams where intense for my machine by utilizing 100% of my ram for 30 minutes straight as seen in the attachment. I want to do more test runs, but before i do that i need to install more ram :)


***

![Eminem](/home/jensruhof/Documents/Computational_musicology/compmus2/struggle.jpg){width=100%}


### Key vs chorodgram analysis of Mockingbird vs outlier Tone deaf

``` {r get_chors}
circshift <- function(v, n) {
  if (n == 0) v else c(tail(v, n), head(v, -n))
}

#      C     C#    D     Eb    E     F     F#    G     Ab    A     Bb    B
major_chord <-
  c(   1,    0,    0,    0,    1,    0,    0,    1,    0,    0,    0,    0)
minor_chord <-
  c(   1,    0,    0,    1,    0,    0,    0,    1,    0,    0,    0,    0)
seventh_chord <-
  c(   1,    0,    0,    0,    1,    0,    0,    1,    0,    0,    1,    0)

major_key <-
  c(6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88)
minor_key <-
  c(6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17)

chord_templates <-
  tribble(
    ~name, ~template,
    "Gb:7", circshift(seventh_chord, 6),
    "Gb:maj", circshift(major_chord, 6),
    "Bb:min", circshift(minor_chord, 10),
    "Db:maj", circshift(major_chord, 1),
    "F:min", circshift(minor_chord, 5),
    "Ab:7", circshift(seventh_chord, 8),
    "Ab:maj", circshift(major_chord, 8),
    "C:min", circshift(minor_chord, 0),
    "Eb:7", circshift(seventh_chord, 3),
    "Eb:maj", circshift(major_chord, 3),
    "G:min", circshift(minor_chord, 7),
    "Bb:7", circshift(seventh_chord, 10),
    "Bb:maj", circshift(major_chord, 10),
    "D:min", circshift(minor_chord, 2),
    "F:7", circshift(seventh_chord, 5),
    "F:maj", circshift(major_chord, 5),
    "A:min", circshift(minor_chord, 9),
    "C:7", circshift(seventh_chord, 0),
    "C:maj", circshift(major_chord, 0),
    "E:min", circshift(minor_chord, 4),
    "G:7", circshift(seventh_chord, 7),
    "G:maj", circshift(major_chord, 7),
    "B:min", circshift(minor_chord, 11),
    "D:7", circshift(seventh_chord, 2),
    "D:maj", circshift(major_chord, 2),
    "F#:min", circshift(minor_chord, 6),
    "A:7", circshift(seventh_chord, 9),
    "A:maj", circshift(major_chord, 9),
    "C#:min", circshift(minor_chord, 1),
    "E:7", circshift(seventh_chord, 4),
    "E:maj", circshift(major_chord, 4),
    "G#:min", circshift(minor_chord, 8),
    "B:7", circshift(seventh_chord, 11),
    "B:maj", circshift(major_chord, 11),
    "D#:min", circshift(minor_chord, 3)
  )

key_templates <-
  tribble(
    ~name, ~template,
    "Gb:maj", circshift(major_key, 6),
    "Bb:min", circshift(minor_key, 10),
    "Db:maj", circshift(major_key, 1),
    "F:min", circshift(minor_key, 5),
    "Ab:maj", circshift(major_key, 8),
    "C:min", circshift(minor_key, 0),
    "Eb:maj", circshift(major_key, 3),
    "G:min", circshift(minor_key, 7),
    "Bb:maj", circshift(major_key, 10),
    "D:min", circshift(minor_key, 2),
    "F:maj", circshift(major_key, 5),
    "A:min", circshift(minor_key, 9),
    "C:maj", circshift(major_key, 0),
    "E:min", circshift(minor_key, 4),
    "G:maj", circshift(major_key, 7),
    "B:min", circshift(minor_key, 11),
    "D:maj", circshift(major_key, 2),
    "F#:min", circshift(minor_key, 6),
    "A:maj", circshift(major_key, 9),
    "C#:min", circshift(minor_key, 1),
    "E:maj", circshift(major_key, 4),
    "G#:min", circshift(minor_key, 8),
    "B:maj", circshift(major_key, 11),
    "D#:min", circshift(minor_key, 3)
  )

```

``` {r get_info_chord}
mockingbird_chord <-
  get_tidy_audio_analysis("0KOYV2jgRZWLcLI94kiscs") %>%
  compmus_align(sections, segments) %>%
  select(sections) %>%
  unnest(sections) %>%
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"
      )
  )

tone_chord <-
  get_tidy_audio_analysis("1cEUi8QulMj1xgrPwwGC2p") %>%
  compmus_align(sections, segments) %>%
  select(sections) %>%
  unnest(sections) %>%
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"
      )
  )

mockingbird_key <-
  get_tidy_audio_analysis("0KOYV2jgRZWLcLI94kiscs") %>%
  compmus_align(sections, segments) %>%
  select(sections) %>%
  unnest(sections) %>%
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"
      )
  )

tone_key <-
  get_tidy_audio_analysis("1cEUi8QulMj1xgrPwwGC2p") %>%
  compmus_align(sections, segments) %>%
  select(sections) %>%
  unnest(sections) %>%
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"
      )
  )

```

``` {r plot_chord_key}
mbc <- mockingbird_chord %>% 
  compmus_match_pitch_template(
    chord_templates,         # Change to chord_templates if descired
    method = "euclidean",  # Try different distance metrics
    norm = "manhattan"     # Try different norms
  ) %>%
  ggplot(
    aes(x = start + duration / 2, width = duration, y = name, fill = d)
  ) +
  geom_tile() +
  # scale_x_discrete(breaks = seq(0, 266.424, by = 60)) +
  # scale_y_discrete(breaks = seq(0, 266.424, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_minimal() +
  labs(x = "Time (s)", y = "") +
  theme(axis.text=element_text(size=5))

tc <- tone_chord %>% 
  compmus_match_pitch_template(
    chord_templates,         # Change to chord_templates if descired
    method = "euclidean",  # Try different distance metrics
    norm = "manhattan"     # Try different norms
  ) %>%
  ggplot(
    aes(x = start + duration / 2, width = duration, y = name, fill = d)
  ) +
  geom_tile() +
  scale_fill_viridis_c(guide = "none") +
  theme_minimal() +
  labs(x = "Time (s)", y = "") +
  theme(axis.text=element_text(size=5))


mbk <- mockingbird_key %>% 
  compmus_match_pitch_template(
    key_templates,         # Change to chord_templates if descired
    method = "euclidean",  # Try different distance metrics
    norm = "manhattan"     # Try different norms
  ) %>%
  ggplot(
    aes(x = start + duration / 2, width = duration, y = name, fill = d)
  ) +
  geom_tile() +
  # scale_x_discrete(breaks = seq(0, 266.424, by = 60)) +
  # scale_y_discrete(breaks = seq(0, 266.424, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_minimal() +
  labs(x = "Time (s)", y = "") +
  theme(axis.text=element_text(size=5))

tk <- tone_key %>% 
  compmus_match_pitch_template(
    key_templates,         # Change to chord_templates if descired
    method = "euclidean",  # Try different distance metrics
    norm = "manhattan"     # Try different norms
  ) %>%
  ggplot(
    aes(x = start + duration / 2, width = duration, y = name, fill = d)
  ) +
  geom_tile() +
  scale_fill_viridis_c(guide = "none") +
  theme_minimal() +
  labs(x = "Time (s)", y = "") +
  theme(axis.text=element_text(size=5))


```




```{r chord_key_grid}
grid.arrange (arrangeGrob(mbc, top=grid::textGrob("Mockingbird", hjust = 0.15), left="Chord"),arrangeGrob(tc,top=grid::textGrob("Tone deaf", hjust = 0.21)),arrangeGrob(mbk, left="Key"),tk,ncol=2, nrow=2, widths = c(2.2,2) ,heights = c(2.2,2))
# ggarrange(mbc, tc, mbk, tk + rremove("x.text"),
#           ncol = 2, nrow = 2)
```




***

#### Explanation of Mockingbird and Tone deaf.

- The keygram for mockingbird is a lot clearer than the chordogram, Mockingbird is know for its piano play, which you can clearly follow over time.
- The grams for tone deaf are totally different and don't seem correct. However tone deaf is I believe the only number of eminem where he doesn't change his sound throughout the song. The interesting thing woth tone deaf is that the same chords are repeated the entire length of the song. 



### timbre and self similarity

```{r ssm info}
mockingbird <-
  get_tidy_audio_analysis("0KOYV2jgRZWLcLI94kiscs") %>% # Change URI.
  compmus_align(bars, segments) %>%                     # Change `bars`
  select(bars) %>%                                      #   in all three
  unnest(bars) %>%                                      #   of these lines.
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "mean", norm = "manhattan"              # Change summary & norm.
      )
  ) %>%
  mutate(
    timbre =
      map(segments,
        compmus_summarise, timbre,
        method = "mean"              # Change summary & norm.
      )
  )
tone <-
  get_tidy_audio_analysis("1cEUi8QulMj1xgrPwwGC2p") %>% # Change URI.
  compmus_align(bars, segments) %>%                     # Change `bars`
  select(bars) %>%                                      #   in all three
  unnest(bars) %>%                                      #   of these lines.
  mutate(
    pitches =
      map(segments,
        compmus_summarise, pitches,
        method = "rms", norm = "manhattan"              # Change summary & norm.
      )
  ) %>%
  mutate(
    timbre =
      map(segments,
        compmus_summarise, timbre,
        method = "mean"              # Change summary & norm.
      )
  )
```

```{r ssm plots}
op <- mockingbird %>%
  compmus_self_similarity(pitches, "manhattan") %>% 
  ggplot(
    aes(
      x = xstart + xduration / 2,
      width = xduration,
      y = ystart + yduration / 2,
      height = yduration,
      fill = d
    )
  ) +
  geom_tile() +
  coord_fixed() +
  scale_x_continuous(breaks = seq(0, 266.424, by = 60)) +
  scale_y_continuous(breaks = seq(0, 266.424, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_classic() +
  labs(x = "", y = "")
ot <- mockingbird %>%
  compmus_self_similarity(timbre, "euclidean") %>% 
  ggplot(
    aes(
      x = xstart + xduration / 2,
      width = xduration,
      y = ystart + yduration / 2,
      height = yduration,
      fill = d
    )
  ) +
  geom_tile() +
  coord_fixed() +
  scale_x_continuous(breaks = seq(0, 266.424, by = 60)) +
  scale_y_continuous(breaks = seq(0, 266.424, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_classic() +
  labs(x = "", y = "")
rp <- tone %>%
  compmus_self_similarity(pitches, "aitchison") %>% 
  ggplot(
    aes(
      x = xstart + xduration / 2,
      width = xduration,
      y = ystart + yduration / 2,
      height = yduration,
      fill = d
    )
  ) +
  geom_tile() +
  coord_fixed() +
  scale_x_continuous(breaks = seq(0, 282.581, by = 60)) +
  scale_y_continuous(breaks = seq(0, 282.812, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_classic() +
  labs(x = "", y = "")
rt <- tone %>%
  compmus_self_similarity(timbre, "euclidean") %>% 
  ggplot(
    aes(
      x = xstart + xduration / 2,
      width = xduration,
      y = ystart + yduration / 2,
      height = yduration,
      fill = d
    )
  ) +
  geom_tile() +
  coord_fixed() +
  scale_x_continuous(breaks = seq(0, 282.812, by = 60)) +
  scale_y_continuous(breaks = seq(0, 282.812, by = 60)) +
  scale_fill_viridis_c(guide = "none") +
  theme_classic() +
  labs(x = "", y = "")
```

```{r ssm grid}
grid.arrange (arrangeGrob(op, top=grid::textGrob("Mockingbird", hjust = 0.15), left="Chroma"),arrangeGrob(rp,top=grid::textGrob("Tone deaf", hjust = 0.21)),arrangeGrob(ot, left="Timbre"),rt,ncol=2, nrow=2, widths = c(2.2,2) ,heights = c(2.2,2), top=grid::textGrob("SSM, based on bars, time in seconds", hjust= 0.42))
```



***

#### week8 text

- figure 1 shows Self-similarity matrices, segmented by bars, with the axes in seconds of the song mockingbird. The timbre based matrix expresses the blocks that can also be seen in chroma more vividly.  around the 90 second mark you see a switch with the timbre, this is also where the refrain of mockingbird starts and more instruments chime in. around the 300 mark the songe ends which can be seen in the chroma matrix, if you look at the crossing yellow lines.
- In figure 2 I wanted to see if a number by Eminem called Tone deaf would yield interesting or different results, and it sure did. This number is one of the few songs of Eminem where the beat stays consistent and the matrices show this.





### Introduction

TODO: Tekst readme plakken
Computational musicology Corpus idea:

How did famous rapper Eminem's style change through te years. From his firsts single "I just dont give a f*ck" in 1998 to one of hist latest songs "The adventures of moon man and slim shady" in 2020.

I chose this corpus, since Eminem is one of my favorite artists of all time. I find myself coming back to his songs on the regular and even went to his last tour in the Netherlands. Eminem has many singles and albums published through the years and all are surounded by some sort of controversion. Eminem even raps about the fact that some of his fans want the old slim shady back, but that he is not that person anymore. I want to check in what degree this statement holds when comparing his music through the years.

Eminem's total of eleven studio albums make a great music library to compare on various aspects. In these albums Eminem changed up his rap game by collabing with artist and go for a more mainstream pop song. I want to see if this has happened more later on or if he did this before as well. Furthermore eminem is known for his many words per second in songs such as "rap god" or "Godzilla"

A possiblie weakness for the corpus might eb that some songs appear on multiple albums. On the one hand this shows that an artist is proud of a certain song and places it in more places. On the other hand i might have to look at the representations of these songs, to not get an unfair advantage or skew the data.

***

Main focus points

- Least popular songs

- Most popular songs

- Interactive radarchart of spotify measurements on eminem

- Chroma feature example of my favourtite eminem song.


***

![Eminem](/home/jensruhof/Documents/Computational_musicology/compmus2/eminem.jpg){width=100%}





### Least popular





``` {r} 
LEAST <- Eminem %>% 
  group_by(track.popularity) %>% 
  filter(track.popularity <= 35) %>%
  ggplot(aes(x =track.popularity , y = fct_reorder(track.name, track.popularity))) + 
  geom_col(aes(fill = track.album.name)) +
  labs(x= "Popularity") + 
  ggtitle("What are the least popular Eminem songs?", "Popularity ranking < 35") +
  theme(legend.position = "right",
        legend.text = element_text( size = 10),
        text = element_text(size = 10),
        axis.title.y = element_blank())

ggplotly(LEAST)
```

***

#### Least popular songs

A thing to note already is that the paul skit appears on multiple albums and 
that his skits often aren't very popular. A reason for this could be that these 
skits aren't songs and are often a story within the album. An example is the 
paul skit or skits I should say. These skits, since there are multiple named 
Paul are a reference to his music manager Paul Rosenberg in which eminem often 
features a snippet of a phone conversation with some information about the album.


### Most popular

``` {r}
most <- Eminem %>% 
  group_by(track.popularity) %>% 
  filter(track.popularity >= 75) %>%
  ggplot(aes(x =track.popularity , y = fct_reorder(track.name, track.popularity))) + 
  geom_col(aes(fill = track.album.name)) +
  labs(x= "Popularity") + 
  ggtitle("What are the most popular Eminem songs?", "Popularity ranking > 75") +
  theme(legend.position = "right",
        legend.text = element_text( size = 10),
        text = element_text(size = 10),
        axis.title.y = element_blank())

ggplotly(most)
```

***

#### Most popular songs of Eminem
An interesting thing to see here is that all of his major albums have at least one song with a high popularity with personally one of his best albums "recovery" having two in it. I would like to delve into popularity per album and compare the populartiy with other things spotify measures later on.

### Radarplot

``` {r}
# test <- plot_ly(
#   type = "scatterpolar",
#   r= c(0.7129386,0.7213708,0.2985401,0.1928719,0.01342138,0.2951325,0.5395462),
#   theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
#   fill = "toself"
# )
# test <- test %>%
#   layout(
#     polar = list(
#       radialaxis = list(
#         visible = T,
#         range = c(0,1)
#       )
#     ),
#     showlegend = F
#   )
# 
# test
fig <- plot_ly(
    type = 'scatterpolar',
    fill = 'toself'
  ) 
fig <- fig %>%
  add_trace(
    r = c(0.7129386, 0.7213708, 0.2985401, 0.1928719, 0.01342138, 0.2951325, 0.5395462),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Overall'
  ) 
fig <- fig %>%
  add_trace(
    r = c(0.7283333, 0.6116333, 0.39715, 0.2735885, 0.05342238, 0.3095833, 0.6580333),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Shady LP'
  )
fig <- fig %>%
  add_trace(
    r = c(0.6994286, 0.7866071, 0.240125, 0.1382441, 0.0007273875, 0.2240429, 0.5306786),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Shady XV'
  )
fig <- fig %>%
  add_trace(
    r = c(0.68, 0.7187368, 0.2828474, 0.26261, 5.927158e-05, 0.3540158, 0.5248947),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Revival'
  )
fig <- fig %>%
  add_trace(
    r = c(0.6959655, 0.7850345, 0.3074414, 0.2102438, 3.313793e-07, 0.3116, 0.558),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Relapse Refill'
  )
fig <- fig %>%
  add_trace(
    r = c(0.7161364, 0.7728636, 0.3247182, 0.2411955, 4.368182e-07, 0.3461591, 0.551),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Relapse Deluxe'
  )
fig <- fig %>%
  add_trace(
    r = c(0.6685294, 0.9168824, 0.2466588, 0.1525941, 1.3e-07, 0.3044941, 0.5481765),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Recovery'
  )
fig <- fig %>%
  add_trace(
    r = c(0.714, 0.66645, 0.219065, 0.1089275, 0.0003467275, 0.31603, 0.431345),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Music to be murdered by side A'
  )
fig <- fig %>%
  add_trace(
    r = c(0.7418056, 0.6893056, 0.2627111, 0.1610169, 0.0004116275, 0.3235917, 0.5114167),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Music to be murdered by side B'
  )
fig <- fig %>%
  add_trace(
    r = c(0.72035, 0.604985, 0.28606, 0.1263365, 0.04794438, 0.17504, 0.504715),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'The Eminem Show'
  )
fig <- fig %>%
  add_trace(
    r = c(0.7386667, 0.6762222, 0.3176167, 0.1434128, 1.096333e-05, 0.3192111, 0.5775444),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'The Marshal Mathers LP'
  )
fig <- fig %>%
  add_trace(
    r = c(0.679125, 0.8578125, 0.3076563, 0.2053562, 0.00239375, 0.4185125, 0.521),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'The Marshal Mathers LP2'
  )
fig <- fig %>%
  add_trace(
    r = c(0.7768462, 0.6326154, 0.3834615, 0.2434692, 0, 0.2661923, 0.5315385),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Kamikaze'
  )
fig <- fig %>%
  add_trace(
    r = c(0.701087, 0.646, 0.2990478, 0.2437839, 0.008007407, 0.2993522, 0.5071261),
    theta = c("mean_danceability", "mean_energy", "mean_speechiness", "mean_acousticness","mean_instrumentalness", "mean_liveness", "mean_valence"),
    name = 'Encore'
  )
fig <- fig %>%
  layout(
    polar = list(
      radialaxis = list(
        visible = T,
        range = c(0,1)
      )
    )
  )

fig
```

***

#### This is a radarchart which represents all the mean values of eminems songs.

- It is interesting that instrumentalness is almost non-existent.

- Energy and danceability are high.

- Speechiness is low, this can be explained by eminemns gimmick. Namely rapping very fast, which is hard to replicate.
- Most of his albums share the same charcteristics, mainly differing in energy.




### Chroma of Mockingbird

```{r}
mockingbird<-
  get_tidy_audio_analysis("561jH07mF1jHuk7KlaeF0s") %>%
  select(segments) %>%
  unnest(segments) %>%
  select(start, duration, pitches)

```
```{r}
mockingbird %>%
  mutate(pitches = map(pitches, compmus_normalise, "euclidean")) %>%
  compmus_gather_chroma() %>% 
  ggplot(
    aes(
      x = start + duration / 2,
      width = duration,
      y = pitch_class,
      fill = value
    )
  ) +
  geom_tile() +
  labs(x = "Time (s)", y = NULL, fill = "Magnitude") +
  theme_minimal() +
  scale_fill_viridis_c() +
  ggtitle("Chroma sampling of Mockingbird using euclidean") +
  theme(title = element_text(size = 9))

```

***

#### Mocking bird

- Mostly played in C D and E


### Results

***
#### Notable things

- Many skits are not popular, still eminem keeps them in his album. This might be since most albums have a story behind them and the skit shows some light on this backstory. An example is the skits with his manager Paul.

- Most of his big albums have atleast one popular song. indicating that he is still performing well even after all those years.

- WHat i still want to do is comapare different albums, especially one of his first vs one of his last and see if there are big differences between them. Sugestions on visualisation are welcome :)